package com.bestcem.xm.ticket.dao.impl.mysql;

import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.TicketWechatNotificationDao;
import com.bestcem.xm.ticket.dao.mapper.TicketWechatNotificationDaoMapper;
import com.bestcem.xm.ticket.entity.mysql.TicketWechatNotificationDO;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ds_ticket_wechat_notification 相关DAO层方法
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version 通知提醒新增微信公众号
 * @date 2022/4/1 18:46
 */
@Slf4j
@Repository
public class TicketWechatNotificationDaoImpl implements TicketWechatNotificationDao {

    @Resource
    private TicketWechatNotificationDaoMapper ticketWechatNotificationDaoMapper;

    /**
     * 查询 通知提醒的订阅微信用户
     *
     * @param surveyId 问卷id
     * @param openId   微信用户openId
     * @return 对应的订阅关系
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/18 17:53
     */
    @Nullable
    @Override
    public TicketWechatNotificationDO findNotificationSubscriber(String surveyId, String openId) {
        return ticketWechatNotificationDaoMapper.selectNotificationSubscriber(surveyId, openId);
    }

    /**
     * 查询 通知提醒的订阅微信用户
     *
     * @param surveyId 问卷id
     * @return 订阅的用户列表
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/7 10:51
     */
    @Nonnull
    @Override
    public List<String> listNotificationSubscriberOpenIds(String surveyId) {
        List<String> result = ticketWechatNotificationDaoMapper.selectNotificationSubscriberOpenIds(surveyId);
        if (CollectionUtils.isEmpty(result)) {
            return new ArrayList<>();
        }
        return result;
    }


    /**
     * 添加 微信用户订阅通知提醒 链接关系
     *
     * @param surveyId 问卷id
     * @param openId   微信openId
     * @return 添加成功与否
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/1 18:52
     */
    @Override
    public boolean saveNotificationSubscription(String surveyId, String openId) {
        TicketWechatNotificationDO isDo = new TicketWechatNotificationDO();
        isDo.setSurveyId(surveyId);
        isDo.setWechatOpenId(openId);
        isDo.setSample(false);
        isDo.setCreateTime(DateUtil.getCommonDate());
        isDo.setUpdateTime(DateUtil.getCommonDate());
        return ticketWechatNotificationDaoMapper.insert(isDo) == 1;
    }


    /**
     * 删除 微信用户订阅通知提醒 链接关系
     *
     * @param surveyId 问卷id
     * @param openIds  微信openId
     * @return 删除成功与否
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/1 18:52
     */
    @NotNull
    @Override
    public Boolean removeNotificationSubscription(String surveyId, List<String> openIds) {
        int count = ticketWechatNotificationDaoMapper.deleteNotificationSubscribers(surveyId, openIds);
        return count > 0;
    }


    /**
     * 移除微信用户订阅的所有问卷
     *
     * @param openId 微信openId
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/11 14:51
     */
    @NotNull
    @Override
    public Boolean removeWechatUserSubscription(String openId) {
        ticketWechatNotificationDaoMapper.deleteWechatUserSubscription(openId);
        return true;
    }
}
